import { HttpClient } from '@angular/common/http';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, Input, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NgxSmartModalService } from 'ngx-smart-modal';
import { Subscription } from 'rxjs/Subscription';
import { environment } from '../../../../../../../../environments/environment';
import { LocationModel } from '../../../../../../../shared/models/location.model';
import { AnalyticsService } from '../../../../../../../shared/services/googleAnalytics.service';
import { UtilsService } from '../../../../../../../shared/services/utils.service';
import { ClusterComponent } from '../../../../../riskCluster/cluster.component';
import { ChartColumnModel } from '../../../../shared/companyIndicator/models/chartColumn.model';
import { CompanyRiskRatingService } from '../../../companyRiskRating.service';
import { EventIndicatorModel } from '../../models/riskEvent.model';
import { GoogleAnalyticsBeacon } from '../../../../../../../shared/models/googleAnalytics.model';
import { ClusterService } from '../../../../../riskCluster/cluster.service';

/**
 * MEI event details component
 *
 * Author: Sorin Spiru
 * Date: 16.01.2018
 */
@Component({
    selector: 'event-details',
    templateUrl: './eventDetails.component.html',
    styleUrls: ['./eventDetails.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class EventDetailsComponent extends ClusterComponent {
    private sub: Subscription;
    public expanded: boolean = false;
    public readonly resourcesUrl: string = environment.resourcesUrl;
    public indicator: EventIndicatorModel;
    public locations: LocationModel[];
    public columns: ChartColumnModel[];

    @Input() eventIndicator: any;
    @Input() eventIndex: number;
    @Input() companyId: string;
    @Input() showAxis: boolean;
    @Input() noExpand: boolean;
    @ViewChild('panelLink') panelLink: ElementRef;

    @Input() set defaultExpanded(value: boolean) {
        if (value == true) {
            //wait till event is rendered before expanding it
            setTimeout(() => {
                this.loadDetails();
            });
        }
    }

    constructor(
        public ngxSmartModalService: NgxSmartModalService,
        private cdRef: ChangeDetectorRef,
        private riskService: CompanyRiskRatingService,
        private analyticsService: AnalyticsService) {
        super(cdRef);
    }

    ngOnInit() {
        this.columns = [
            new ChartColumnModel('Raw Score', 20, 100, 'score', false, '#B3D4D1', true, false, true, '', null, 0),
            new ChartColumnModel('Weight', 20, 100, 'weight', false, '#B5B7B7', true, false, true, '%', null, 2),
            new ChartColumnModel('Weighted Score', 30, 100, 'weightedScore',  false, 'black', true, true, true, '', null, 1)];
    }

    public loadDetails(): void {
        if(this.eventIndicator.category > 0){
            this.expanded = !this.expanded;            
            this.analyticsService.sendBeacon(new GoogleAnalyticsBeacon('ESG RR Events', 'ESG Risk Rating', { CompanyId: this.companyId, Code: this.eventIndicator.code }));
            if (this.expanded && this.indicator == undefined) {
                this.riskService.getEventsByCode(this.companyId, this.eventIndicator.code).subscribe((data) => {
                    this.indicator = data;                    
                    this.cdRef.markForCheck();
                });

                this.riskService.getLocationByEventCode(this.companyId, this.eventIndicator.code).subscribe((data) => {
                    this.locations = data;
                    this.cdRef.markForCheck();
                });
            }        
            this.ngxSmartModalService.getModal('indicatorEvent' + this.getCleanIndicatorCode(this.eventIndicator.code)).open();
        }
    }


    getCategoryIcon(indicator) {
        return UtilsService.getIconFilename('icon-cat' + indicator.category);
    }

    public getCleanIndicatorCode( code ) {
        return UtilsService.getCleanIndicatorCode(code);
    }

    public splitTags(tags: string) {
        return tags.split(',');
    }
}